/**
 * Design By: CEC GienTech Technology Co.,Ltd. All rights reserved.
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ccdc.cbpc.bigdata.common.util;

import com.ccdc.cbpc.bigdata.open.common.enums.RespCodeEnum;
import com.ccdc.cbpc.bigdata.open.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * 请尽可能详细的描述定义此类的目的、提供的功能及使用建议。
 * @title com.pactera.data.common.util.BeanUtils
 * @description BeanUtils
 * @author <a href="kido@pactera.com">kido</a>
 * @date 2021/8/6
 * @since 1.0.0
 */
@Slf4j
public class CommonBeanUtils extends org.springframework.beans.BeanUtils {


    /**
     * 对象拷贝工具，仅需提供类型的拷贝方法
     * @param source
     * @param targetClass
     * @return
     * @throws BusinessException
     */
    public static Object copyProperties(Object source, Class targetClass) throws BusinessException {
        Assert.notNull(source, "Source must not be null");
        Object target = null;
        try {
            target = targetClass.newInstance();
            copyProperties(source, target);
        } catch (Exception e) {
            log.error("Bean 拷贝失败!", e);
            throw new BusinessException(RespCodeEnum.OTHER);
        }
        return target;
    }

    /**
     * @param source 要复制的来源
     * @param target 要复制的目标类型
     * @return
     * @throws
     * @description List对象拷贝工具
     * @auth dong.zhang8
     * @date 2021/8/6
     **/
    @SuppressWarnings("")
    public static List<Object> copyListProperties(List source, Class target) throws BusinessException {
        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");
        List targetlist = new ArrayList();
        source.forEach(o -> {
            try {
                Object targetObject = target.newInstance();
                copyProperties(o, targetObject);
                targetlist.add(targetObject);
            } catch (Exception e) {
                log.error("Bean 拷贝失败!", e);
                throw new BusinessException(RespCodeEnum.OTHER);
            }
        });
        return targetlist;
    }
}